﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using fmtx.Framework.Img;
using SYH.Domain.Common;
using fmtx.Framework.DALFactory;
using SYH.IBLL;
using System.IO;
using SYH.Domain;
using SYH.Web.Models;

namespace SYH.Web.Controllers.Common
{
    public class ImgController : BaseController
    {
        private readonly IEntityRepository<AccAssociation> _Attaervice;
        private readonly IEntityRepository<BusinessFile> _BFervice;
        private readonly string tempPath = ConfigurationManager.AppSettings["DefaultTempUploadUrl"];
        private readonly string SincerityValicode = ConfigurationManager.AppSettings["SincerityValicode"];
        public ImgController(
            IEntityRepository<AccAssociation> Attaervice,
            IEntityRepository<BusinessFile> BFervice)
        {
            _Attaervice = Attaervice;
            _BFervice = BFervice;
        }
        // GET: Img
        /// <summary>
        /// 获取图片缩略图
        /// </summary>
        /// <param name="id"></param>
        /// <param name="m"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <returns></returns>
        [OutputCache(Duration = 720 * 60)]
        public ActionResult Index(string id, ImageFactory.ImgModel m, int w = 100, int h = 100)
        {
            try
            {
                var Suffix = "";
                var path = PublicCs.GetFile(Guid.Parse(id),out Suffix);
                return File(ImageFactory.MakeThumbnail(Server.MapPath(path), w, h, m), "image/" + (Suffix.Substring(1)));
            }
            catch (Exception)
            {
            }
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "No-Cache");
            Response.Cache.SetNoStore();
            return File(ImageFactory.MakeThumbnail(Server.MapPath("~/") + "Images/FileDefault.jpg", w, h, m), "image/jpg");
        }

        /// <summary>
        /// 输出图片
        /// </summary>
        /// <param name="f"></param>
        /// <param name="m"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <returns></returns>
        [OutputCache(Duration = 720 * 60)]
        public ActionResult Pic(Guid id, ImageFactory.ImgModel m, int w = 100, int h = 100)
        {
            try
            {
                var image = PublicCs.GetFileBId(id);
                var filepath = Server.MapPath(image);
                if (System.IO.File.Exists(filepath))
                {
                    return File(ImageFactory.MakeThumbnail(filepath, w, h, m), "image/png");
                }
            }
            catch (Exception)
            {
            }
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "No-Cache");
            Response.Cache.SetNoStore();
            return File(ImageFactory.MakeThumbnail(Server.MapPath("~/") + "Images/FileDefault.jpg", w, h, m), "image/jpg");
        }

        /// <summary>
        /// 输出图片
        /// </summary>
        /// <param name="f"></param>
        /// <param name="m"></param>
        /// <param name="w"></param>
        /// <param name="h"></param>
        /// <returns></returns>
        [OutputCache(Duration = 720 * 60)]
        public ActionResult word(string path,string id, ImageFactory.ImgModel m, int w = 100, int h = 100)
        {
            try
            {
                var image = tempPath+@"\viewTemp\";
                var filepath = Server.MapPath(image+ path+@"\"+id+".png");
                if (System.IO.File.Exists(filepath))
                {
                    return File(ImageFactory.MakeThumbnail(filepath, w, h, m), "image/png");
                }
            }
            catch (Exception)
            {
            }
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "No-Cache");
            Response.Cache.SetNoStore();
            return File(ImageFactory.MakeThumbnail(Server.MapPath("~/") + "Images/FileDefault.jpg", w, h, m), "image/jpg");
        }
    }
}